package com.alibaba.citrus.cr.unified.inventory.functions.consignmentOrder;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crunifiedinventory.dto.request.ConsignmentOrderWriteCreateRequest;
import com.epoch.app.crunifiedinventory.sdo.ConsignmentOrderLineSDO;
import com.epoch.app.crunifiedinventory.sdo.ConsignmentOrderSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ConsignmentOrderFactory {
    private static final Log log = Log.getLogger(ConsignmentOrderFactory.class);
    private ConsignmentOrderSDO consignmentOrderSDO;

    public static ConsignmentOrderEntity createConsignmentOrder(ConsignmentOrderWriteCreateRequest request) {
        ConsignmentOrderSDO consignmentOrderSDO = new ConsignmentOrderSDO();
        BeanUtils.copyProperties(request,consignmentOrderSDO);
        consignmentOrderSDO.setOuterDeliveryOrderId(request.getOuterDeliveryOrderId());
        consignmentOrderSDO.setOuterSalesOrderId(request.getOuterSalesOrderId());
        consignmentOrderSDO.setSalesOrderId(request.getSalesOrderId());
        consignmentOrderSDO.setOuterStoOrderId(request.getOuterStoOrderId());
        consignmentOrderSDO.setStoOrderId(request.getStoOrderId());
        consignmentOrderSDO.setSalesOrgId(request.getSalesOrgId());
        consignmentOrderSDO.setCreator(request.getCreator());
        consignmentOrderSDO.setCreatDate(request.getCreatDate());
        consignmentOrderSDO.setModifier(request.getModifier());
        consignmentOrderSDO.setModifidDate(request.getModifidDate());
        consignmentOrderSDO.setDeliveryDate(request.getDeliveryDate());
        consignmentOrderSDO.setFeatures(request.getFeatures());
        List<ConsignmentOrderLineSDO> orderLineSDOS = request.getConsignmentOrderLineRequest().stream().map(orderLineRequest -> {
            ConsignmentOrderLineSDO orderLineSDO = new ConsignmentOrderLineSDO();
            BeanUtils.copyProperties(orderLineRequest,orderLineSDO);
            orderLineSDO.setOuterOrderLineId(orderLineRequest.getOuterOrderLineId());
            orderLineSDO.setOuterSoLineId(orderLineRequest.getOuterSoLineId());
            orderLineSDO.setSoLineId(orderLineRequest.getSoLineId());
            orderLineSDO.setOuterStoLineId(orderLineRequest.getOuterStoLineId());
            orderLineSDO.setStoLineId(orderLineRequest.getStoLineId());
            orderLineSDO.setScItemId(orderLineRequest.getScItemId());
            orderLineSDO.setQty(orderLineRequest.getQty());
            orderLineSDO.setPodStatus(orderLineRequest.getPodStatus());
            orderLineSDO.setPodQty(orderLineRequest.getPodQty());
            orderLineSDO.setFactoryCode(orderLineRequest.getFactoryCode());
            orderLineSDO.setInventoryCode(orderLineRequest.getInventoryCode());
            orderLineSDO.setCreator(request.getCreator());
            orderLineSDO.setModifier(request.getModifier());
            orderLineSDO.setSaleUnit(orderLineRequest.getSaleUnit());
            orderLineSDO.setBasicQuantity(orderLineRequest.getBasicQuantity());
            return orderLineSDO;
        }).collect(Collectors.toList());
        consignmentOrderSDO.setConsignmentOrderLineSDOS(orderLineSDOS);
        return ConsignmentOrderEntity.of(consignmentOrderSDO);
    }

    public static ConsignmentOrderEntity deleteConsignmentOrder(ConsignmentOrderSDO query) {
        return ConsignmentOrderEntity.of(query);
    }

    public static ConsignmentOrderEntity createConsignmentOrderCopy033Param(ConsignmentOrderWriteCreateRequest request, ConsignmentOrderSDO query) {
        log.info("ConsignmentOrderFactory createConsignmentOrder request={},query={}", JSON.toJSONString(request),JSON.toJSONString(query));
        ConsignmentOrderSDO consignmentOrderSDO = new ConsignmentOrderSDO();
        BeanUtils.copyProperties(request,consignmentOrderSDO);
        consignmentOrderSDO.setOuterDeliveryOrderId(request.getOuterDeliveryOrderId());
        consignmentOrderSDO.setOuterSalesOrderId(request.getOuterSalesOrderId());
        consignmentOrderSDO.setSalesOrderId(request.getSalesOrderId());
        consignmentOrderSDO.setOuterStoOrderId(request.getOuterStoOrderId());
        consignmentOrderSDO.setStoOrderId(request.getStoOrderId());
        consignmentOrderSDO.setSalesOrgId(request.getSalesOrgId());
        consignmentOrderSDO.setCreator(request.getCreator());
        consignmentOrderSDO.setCreatDate(request.getCreatDate());
        consignmentOrderSDO.setModifier(request.getModifier());
        consignmentOrderSDO.setModifidDate(request.getModifidDate());
        consignmentOrderSDO.setDeliveryDate(request.getDeliveryDate());
        //features combination
        if(!StringUtils.isEmpty(request.getFeatures()) && !StringUtils.isEmpty(query.getFeatures())){
            String features = request.getFeatures()+query.getFeatures();
            if(query.getFeatures().contains("billStatus:C;")){
                features.replaceAll("billStatus:A;","");
            }
            consignmentOrderSDO.setFeatures(features.replaceAll(";;",";"));
        }else  if(StringUtils.isEmpty(request.getFeatures()) && !StringUtils.isEmpty(query.getFeatures())){
            consignmentOrderSDO.setFeatures(query.getFeatures());
        }else {
            consignmentOrderSDO.setFeatures(request.getFeatures());
        }


        //copy 033
        consignmentOrderSDO.setOutMaterialCertificateId(query.getOutMaterialCertificateId());
        consignmentOrderSDO.setOutboundOrderNo(query.getOutboundOrderNo());
        consignmentOrderSDO.setPickingStatus(query.getPickingStatus());
        consignmentOrderSDO.setDeliverType(query.getDeliverType());
        consignmentOrderSDO.setTransportSite(query.getTransportSite());
        consignmentOrderSDO.setTransportRouteCode(query.getTransportRouteCode());
        consignmentOrderSDO.setTransportRouteDesc(query.getTransportRouteDesc());
        consignmentOrderSDO.setNetWeight(query.getNetWeight());
        consignmentOrderSDO.setGrossWeight(query.getGrossWeight());
        consignmentOrderSDO.setVolume(query.getVolume());
        consignmentOrderSDO.setWeightUnit(query.getWeightUnit());
        consignmentOrderSDO.setVolumeUnit(query.getVolumeUnit());
        consignmentOrderSDO.setTransportRequire(query.getTransportRequire());
        consignmentOrderSDO.setSendToCode(query.getSendToCode());
        consignmentOrderSDO.setSoldToCode(query.getSoldToCode());
        consignmentOrderSDO.setDistributeChannel(query.getDistributeChannel());
        consignmentOrderSDO.setDepartment(query.getDepartment());
        consignmentOrderSDO.setDeliverPriority(query.getDeliverPriority());
        consignmentOrderSDO.setDeliverPlanDate(query.getDeliverPlanDate());
        consignmentOrderSDO.setSendPlanDate(query.getSendPlanDate());
        consignmentOrderSDO.setRemark(query.getRemark());
        Map<String, ConsignmentOrderLineSDO> outOrderLineIdMap = query.getConsignmentOrderLineSDOS().stream().collect(Collectors.toMap(ConsignmentOrderLineSDO::getOuterOrderLineId, Function.identity(), (p1, p2) -> p2));
        log.info("ConsignmentOrderFactory createConsignmentOrder outOrderLineIdMap={}", JSON.toJSONString(outOrderLineIdMap));
        List<ConsignmentOrderLineSDO> orderLineSDOS = request.getConsignmentOrderLineRequest().stream().map(orderLineRequest -> {
            ConsignmentOrderLineSDO orderLineSDO = new ConsignmentOrderLineSDO();
            BeanUtils.copyProperties(orderLineRequest,orderLineSDO);
            //copy 033
            if(outOrderLineIdMap.containsKey(orderLineRequest.getOuterOrderLineId())){
                ConsignmentOrderLineSDO exist = outOrderLineIdMap.get(orderLineRequest.getOuterOrderLineId());
                orderLineSDO.setSapBatchNo(exist.getSapBatchNo());
                orderLineSDO.setBaseUnit(exist.getBaseUnit());
                orderLineSDO.setNetWeight(exist.getNetWeight());
                orderLineSDO.setGrossWeight(exist.getGrossWeight());
                orderLineSDO.setVolumeGoods(exist.getVolumeGoods());
                orderLineSDO.setAskProductionDate(exist.getAskProductionDate());
                orderLineSDO.setActProductionDate(exist.getActProductionDate());
                orderLineSDO.setSapUpLevelLineNo(exist.getSapUpLevelLineNo());
            }
            orderLineSDO.setOuterOrderLineId(orderLineRequest.getOuterOrderLineId());
            orderLineSDO.setOuterSoLineId(orderLineRequest.getOuterSoLineId());
            orderLineSDO.setSoLineId(orderLineRequest.getSoLineId());
            orderLineSDO.setOuterStoLineId(orderLineRequest.getOuterStoLineId());
            orderLineSDO.setStoLineId(orderLineRequest.getStoLineId());
            orderLineSDO.setScItemId(orderLineRequest.getScItemId());
            orderLineSDO.setQty(orderLineRequest.getQty());
            orderLineSDO.setPodStatus(orderLineRequest.getPodStatus());
            orderLineSDO.setPodQty(orderLineRequest.getPodQty());
            orderLineSDO.setFactoryCode(orderLineRequest.getFactoryCode());
            orderLineSDO.setInventoryCode(orderLineRequest.getInventoryCode());
            orderLineSDO.setCreator(request.getCreator());
            orderLineSDO.setModifier(request.getModifier());
            orderLineSDO.setSaleUnit(orderLineRequest.getSaleUnit());
            orderLineSDO.setBasicQuantity(orderLineRequest.getBasicQuantity());
            return orderLineSDO;
        }).collect(Collectors.toList());
        consignmentOrderSDO.setConsignmentOrderLineSDOS(orderLineSDOS);
        return ConsignmentOrderEntity.of(consignmentOrderSDO);
    }

}
